home *** CD-ROM | disk | FTP | other *** search
/ Alde ADA 1: #1 / CCCC 8804 Volume 1 Number 1 - Alde.iso / C / MISC / FUNC / PROFF.ARC / PXLEX.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-02-21  |  4.3 KB  |  198 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <ctype.h>
  4.  
  5. /* translation table for control chars */
  6.  
  7. char c_ctrl[] = {
  8.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  10.    0, 0, 0, 0, 0, 0, 0, 0,
  11.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12.    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13.    0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
  14.    10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
  15.    23, 25, 26, 27, 28, 29, 30, 31, 0, 1, 2, 3, 4, 5,
  16.    6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
  17.    20, 21, 22, 23, 24, 25, 26, 0, 0, 0, 0, 0
  18. };
  19.  
  20. /*
  21.  * getval - evaluate optional numeric argument
  22.  *
  23.  * increments i
  24.  */
  25. int getval(buf, i, argtyp)
  26. char buf[];
  27. int *i;
  28. int *argtyp;
  29. {
  30.    int j, k;
  31.  
  32.    j = *i;
  33.    k = *argtyp;
  34.  
  35.    skipbl(buf, &j);
  36.    k = buf[j];
  37.    if (k == '+' || k == '-')
  38.       j++;
  39.    *i = j;
  40.    *argtyp = k;
  41.    return (ctoi(buf, i));
  42. }
  43.  
  44. /*
  45.  * getarg - get the next argument from the buffer
  46.  *
  47.  * return values:      -1 - no argument
  48.  *                      n - number of chars in argument
  49.  *
  50.  * also handles quoted ("..") strings. If a quote is wanted
  51.  * in the string, use "" or \". quotes are stripped.
  52.  *
  53.  * argument delimiters: blank, tab or comma (,).
  54.  *
  55.  * increments i
  56.  *
  57.  */
  58. int getarg(buf, i, arg)
  59. char buf[];
  60. int *i;
  61. char arg[];
  62. {
  63.    int j, k;
  64.    register char ch;
  65.  
  66.    j = *i;
  67.  
  68.    k = -1;
  69.    skipbl(buf, &j);
  70.    if (buf[j] != '\0')
  71.    {
  72.       k = 0;
  73.       if (buf[j] == '\"')
  74.       {
  75.          j++;
  76.          while (buf[j] != '\0')
  77.          {
  78.             if (buf[j] == '\"')
  79.             {
  80.                if (buf[j + 1] == '\"')
  81.                {
  82.                   arg[k++] = '\"';
  83.                   j += 2;
  84.                }
  85.                else
  86.                   break;
  87.             }
  88.             arg[k++] = buf[j++];
  89.          }
  90.          arg[k] = '\0';
  91.          j++;                          /* skip the quote */
  92.          /* peek next char */
  93.          if (isalnum(buf[j]))
  94.             error("improper argument list.");
  95.          j++;                          /* skip the delimeter */
  96.       }
  97.       else
  98.       {
  99.          ch = buf[j];
  100.          while (ch != ' ' &&
  101.                 ch != '\t' &&
  102.                 ch != ',' &&
  103.                 ch != '\r' &&
  104.                 ch != '\n' &&
  105.                 ch != '\0')
  106.          {
  107.             arg[k++] = buf[j++];
  108.             ch = buf[j];
  109.          }
  110.          arg[k] = '\0';
  111.          if (ch != '\0')               /* if non-null delimiter, skip */
  112.             j++;
  113.       }
  114.       *i = j;
  115.    }
  116.    return (k);
  117. }
  118.  
  119. /*
  120.  * getpstr - get a special string to print out
  121.  *
  122.  */
  123. getpstr(buf, out)
  124. register char *buf;
  125. register char *out;
  126. {
  127.    register int i;
  128.    register char c, cc;
  129.    register char *num;
  130.    char numbuf[9];
  131.  
  132.    while (*buf != '\n' && *buf != '\0')
  133.    {
  134.       c = *buf;
  135.       switch (c)
  136.       {
  137.       case ' ':
  138.       case '\t':
  139.          while (*buf == ' ' || *buf == '\t')
  140.             buf++;                     /* skip blanks */
  141.          break;
  142.       case '\\':
  143.          if (*(buf + 1) != '\0')
  144.          {
  145.             *out++ = *(buf + 1);
  146.             buf += 2;
  147.          }
  148.          else
  149.             buf++;
  150.          break;
  151.       case '^':
  152.          if ((cc = c_ctrl[*(buf + 1)]) != 0)
  153.             *out++ = cc;
  154.          buf += 2;
  155.          break;
  156.       case '\"':
  157.          buf++;                        /* skip the quote */
  158.          while (*buf != '\0')
  159.          {
  160.             if (*buf != '\"')
  161.                *out++ = *buf++;
  162.             else if (*(buf + 1) == '\"')
  163.             {
  164.                *out++ = '\"';
  165.                buf += 2;
  166.             }
  167.             else
  168.                break;
  169.          }
  170.          buf++;                        /* skip the quote */
  171.          break;
  172.       case '0':
  173.       case '1':
  174.       case '2':
  175.       case '3':
  176.       case '4':
  177.       case '5':
  178.       case '6':
  179.       case '7':
  180.       case '8':
  181.       case '9':
  182.          num = numbuf;
  183.          while (isdigit(*buf))
  184.             *num++ = *buf++;
  185.          *num = '\0';
  186.          if ((i = atoi(numbuf)) > 256)
  187.             error("non-ascii char value in write string.");
  188.          else
  189.          if (i > 0)                    /* do not output null */
  190.             *out++ = (char) i;
  191.          break;
  192.       default:
  193.          *out++ = *buf++;
  194.       }
  195.    }
  196.    *out = '\0';
  197. }
  198.